class Solution:
    def areSentencesSimilar(self, sentence1: str, sentence2: str) -> bool:
        words1 = sentence1.split()
        words2 = sentence2.split()
        front = 0
        back1 = len(words1) - 1
        back2 = len(words2) - 1
        while front < len(words1) and front < len(words2) and words1[front] == words2[front]:
            front += 1
        while back1 >= 0 and back2 >= 0 and words1[back1] == words2[back2]:
            back1 -= 1
            back2 -= 1
        return back1 < front or back2 < front
